#!/usr/bin/env python

import os, sys, subprocess, traci

from TLSController import TLSController

###
## TODO parse command line
##
###

PORT = 8813
SUMO_EXE = "sumo-gui"
CONFIG_FILE = "../models/campinas/campinas.sumo.cfg"

def getSumoPath(exe) :
    sumoExe = exe
    
    if "SUMO" in os.environ:
        sumoExe = os.path.join(os.environ["SUMO"], "sumo-gui")
        
    return sumoExe

def startSumo(exe, configFile) :
    sumoProcess = subprocess.Popen("%s -c %s" % (exe, configFile), shell=True, stdout=sys.stdout)
    return sumoProcess

"""
" Inicializa o simulador e conecta com sua interface traci
"""
startSumo(getSumoPath(SUMO_EXE), CONFIG_FILE)
traci.init(PORT)

traci.simulationStep(1);
tls = TLSController("campossalesxglicerio");

for step in range(3000) :
    traci.simulationStep(step)
    tls.onStep(step)
    #traci.trafficlights.setPhaseDuration("campossalesxglicerio", 1000)
    #print traci.lane.getLastStepHaltingNumber("campossalesSeg2_0")
    #print traci.trafficlights.getPhase("campossalesxglicerio")

traci.close()